<?php
/**
 * 网络请求类
 * User: kang
 * Date: 2020-03-02
 * Time: 21:04
 */

namespace mylibrary;


class NetworkRequest
{

    /**
     * 封装curl,get方式
     * @param string $url 地址
     * @return string 结果
     */
    public function curl_get($url){
        /*初始化*/
        $ch = curl_init();
        /*设置选项，包括URL*/
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        /*执行并获取HTML文档内容*/
        $output = curl_exec($ch);
        /*释放curl句柄*/
        curl_close($ch);
        /*返回获得的数据*/
        return $output;
    }

    /**
     * 封装curl,post方式
     * @param string $url 地址
     * @param array $data 数据数组
     * @return string 结果
     */
    public function curl_post($url,$data){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        return $return;
    }
}